package com.dragon.repository.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.dragon.repository.entity.Produce;
import com.dragon.repository.entity.QuotesProducts;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 供应商产品报价关联表 Mapper 接口
 * </p>
 *
 * @author sy
 * @since 2023-08-17
 */
@Mapper
public interface QuotesProductsMapper extends BaseMapper<QuotesProducts> {

    @Select("SELECT a.*,b.`name` as name ,b.specification ,c.`name` as unitName, b.material as material FROM dragon_quotes_products a  " +
            "LEFT JOIN dragon_vendor_products b ON  a.products_id = b.id " +
            "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND b.unit = c.id " +
            "WHERE a.quotes_id = #{id}")
    List<QuotesProducts> selectProductsList(Integer id);


    @Select("SELECT a.*,b.`name` as name ,c.`name` as unitName FROM dragon_quotes_products a  " +
            "LEFT JOIN dragon_vendor_products b ON  a.products_id = b.id " +
            "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND b.unit = c.id " +
            "LEFT JOIN  dragon_contract e on e.quotes_id = a.quotes_id WHERE e.id = #{contractId}")
    List<QuotesProducts> selectListByContractId(Integer contractId);

    @Select("SELECT a.*,b.`name` as name ,b.specification ,c.`name` as unitName, b.material as material FROM dragon_quotes_products a " +
            "LEFT JOIN dragon_quotes dq on dq.id = a.quotes_id " +
            "LEFT JOIN dragon_vendor_products b ON  a.products_id = b.id " +
            "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND b.unit = c.id " +
            "${ew.customSqlSegment} " )
    List<QuotesProducts> selectProductsListInQuotesId(@Param(Constants.WRAPPER) QueryWrapper<QuotesProducts> wrapper);

    @Select("SELECT M.*,N.order_time,N.num contractCode,N.money-N.refund_money contractMoney FROM " +
            "(SELECT a.*,b.`name` as name ,b.specification ,c.`name` as unitName, b.material as material FROM dragon_quotes_products a " +
            "LEFT JOIN dragon_quotes dq on dq.id = a.quotes_id " +
            "LEFT JOIN dragon_vendor_products b ON  a.products_id = b.id " +
            "LEFT JOIN dragon_dictsort_detail c ON c.code_type = 'unitCode' AND b.unit = c.id)M " +
            "LEFT JOIN dragon_contract N on M.quotes_id = N.quotes_id " +
            "${ew.customSqlSegment} " )
    List<QuotesProducts> selectContractAndProductsListInQuotesId(@Param(Constants.WRAPPER) QueryWrapper<QuotesProducts> wrapper);
}
